package com.penghaisoft.microwms.producthand.web.controller.intowarehouse;

import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.dto.HandResp;
import com.penghaisoft.microwms.common.dto.ProOrderInSnDto;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.product.service.IProOrderInSnService;

/**
 * @Description 成品下线入库
 * @author luoteng
 * @date 2018年6月7日 上午10:00:37 
 * @version 1.0
 */
@RestController
@RequestMapping(value = "/pro/downline/intowh")
public class DownlineIntoWhController extends BaseController {

	@Autowired
	private IProOrderInSnService proOrderInSnService;
	
	@Resource
	private StringRedisTemplate stringRedisTemplate;//注入对象
	
	/** 
	 * @Title scanBarcode
	 * @Description 扫码入库
	 * @author luoteng
	 * @date 2018年6月7日:上午10:03:52
	 * @param proOrderInSnDto
	 * @return HandResp
	 */
	@PostMapping("scan")
    public HandResp scanBarcode(@RequestBody ProOrderInSnDto proOrderInSnDto){
		HandResp resp = new HandResp();
		String key = Constant.PRO_DOWNLINEIN_PREFIX + proOrderInSnDto.getBarcode();
		
		if (proOrderInSnDto.getBarcode() == null || proOrderInSnDto.getBarcode().equals("")){
        	resp.setCode(Constant.FAIL);
        	resp.setMsg("请扫描条码！");
            return resp;
        }
		
		if(stringRedisTemplate.hasKey(key)){
			resp.setCode(Constant.FAIL);
			resp.setMsg("当前条码正在处理中，请不要重复扫描！");
			return resp;
		}else{
			stringRedisTemplate.opsForValue().set(key, "1", Constant.PRO_HAND_LOST_MINUTES, TimeUnit.MINUTES);//写入对象，并设置失效时间
			
			TransResult result = proOrderInSnService.downlineScanBarcode(proOrderInSnDto);
			resp.setCode(result.getCode());
	        resp.setMsg(result.getMsg());
	        resp.setList(result.getList());
	        
	        stringRedisTemplate.delete(key);//删除对象
		}
		
		return resp;
	}
	
	/** 
	 * @Title delBarcode
	 * @Description 退扫条码【暂时不用】
	 * @author luoteng
	 * @date 2018年6月7日:上午10:04:02
	 * @param proOrderInSnDto
	 * @return HandResp
	 */
	@PostMapping("del")
    public HandResp delBarcode(@RequestBody ProOrderInSnDto proOrderInSnDto){
		HandResp resp = new HandResp();
		String key = Constant.PRO_DOWNLINEIN_PREFIX + proOrderInSnDto.getBarcode();
		
		if (proOrderInSnDto.getBarcode() == null || proOrderInSnDto.getBarcode().equals("")){
        	resp.setCode(Constant.FAIL);
        	resp.setMsg("请扫描条码！");
            return resp;
        }
		
		if(stringRedisTemplate.hasKey(key)){
			resp.setCode(Constant.FAIL);
			resp.setMsg("当前条码正在处理中，请不要重复扫描！");
			return resp;
		}else{
			stringRedisTemplate.opsForValue().set(key, "1", Constant.PRO_HAND_LOST_MINUTES, TimeUnit.MINUTES);//写入对象，并设置失效时间
			
			TransResult result = proOrderInSnService.downlineDelBarcode(proOrderInSnDto);
			resp.setCode(result.getCode());
	        resp.setMsg(result.getMsg());
	        resp.setList(result.getList());
	        
	        stringRedisTemplate.delete(key);//删除对象
		}
		
		return resp;
	}
}
